﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrelloNet;

namespace SprintBoardMaker.Data
{
    public class TrelloAccess : ITrelloAccess
    {
        private static TrelloAccess _instance;
        public static TrelloAccess Instance
        {
            set { _instance = value; }
            get
            {
                if (_instance == null)
                {
                    _instance = new TrelloAccess();
                    _instance.UsersBoards = _instance.GetBoardsForCurrentUser(BoardFilter.Open);
                }
                return _instance;
            }
        }

        TrelloNet.Trello _trello;
        public Uri AuthorizationURL { get; set; }

        public TrelloAccess()
        {
            _trello = new Trello(GetTrelloAPIKey());
            AuthorizationURL = _trello.GetAuthorizationUrl("Sprint Board Maker (try 3)", Scope.ReadWrite, Expiration.OneHour);
        }

        public void CreateBoard(string name, NormalizedBoard boardToCreate)
        {
            throw new NotImplementedException();
        }

        public List<NormalizedBoard> GetBoardsForCurrentUser(BoardFilter filter)
        {
            return new List<NormalizedBoard>();
            // Uncomment if passing in sOrgID
            //List<Board> trelloBoards;
            //if (!String.IsNullOrEmpty(sOrgID))
            //    trelloBoards = _trello.Boards.ForMember(member).Where((board) => board.IdOrganization == sOrgID).ToList<Board>();
            //else
                //trelloBoards = ;

            List<NormalizedBoard> normalizedBoards = new List<NormalizedBoard>();
            foreach (Board b in _trello.Boards.ForMe().ToList<Board>())
            {
                normalizedBoards.Add(new NormalizedBoard(b));
            }

            return normalizedBoards;
        }

        public List<TrelloNet.List> GetListsForBoard(string boardID)
        {
            Board board = _trello.Boards.WithId(boardID);
            if (board == null)
                throw new Exception();      // TODO: error handling

            return _trello.Lists.ForBoard(board).ToList<TrelloNet.List>();
        }
        
        public List<TrelloNet.Member> GetMembersForBoard(string boardID)
        {
            Board board = _trello.Boards.WithId(boardID);
            if (board == null)
                throw new Exception();      // TODO: error handling

            return _trello.Members.ForBoard(board).ToList<TrelloNet.Member>();
        }

        public List<TrelloNet.Member> GetOrgMembers(string orgID)
        {
            throw new NotImplementedException();
        }

        public string GetRoydanOrgID()
        {
            throw new NotImplementedException();
        }

        private string GetTrelloAPIKey()
        {
            // TODO: get from config
            return "d8b74c808ae490e50569710c266a99bf";
        }

        public string GetTrelloAuthID()
        {
            throw new NotImplementedException();
        }

        public void SaveAuthKey(string authKey)
        {
            // TODO: Implement
            //throw new NotImplementedException();

            return;
        }

        public void SetRoydanOrgID(string roydanOrgID)
        {
            throw new NotImplementedException();
        }

        public List<NormalizedBoard> UsersBoards { get; set; }

        string ITrelloAccess.GetTrelloAPIKey()
        {
            throw new NotImplementedException();
        }

        public Uri GetAuthorizationURI()
        {
            return this.AuthorizationURL;
        }
    }
}
